require 'map_navigator'
require 'map_editor/map_panel'
require 'map_editor/io'
require 'event_editor/map_move_controler'
module EventEditor
  module Commands
    class MoveMap
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Map Move",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @map_panel = nil
        @model = model
        box = Gtk::HBox.new

        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all
     
        @dialog.set_size_request(800, 600)
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.create_panel
        @map_container = Gtk::HBox.new
        
        @map_navigator = MapNavigator.new(self, 300)
        @map_navigator.set_width_request(200)
        @map_navigator.load

        @map_container.pack_start(@map_navigator, false, false)
        
        return @map_container
      end
      
      def self.load_map(map_model, event_model, sx = 0, sy = 0)
        @map_model = map_model
        @map_container.remove(@map_panel) if @map_panel
        @controler = MapMoveControler.new(map_model, sx, sy)
        @map_panel = MapEditor::MapPanel.new(map_model, 2, {:map_move => @controler}, :map_move, 0, false)
        @map_container.add(@map_panel)
        @dialog.vbox.show_all
        GC.start
      end
      
      def self.show_all
      
      end
      
      def self.load(edit_command)
        if edit_command.empty?
          map_model = MapEditor::IO.load(@model.map_model.map_id)
          self.load_map(map_model, nil)
        else
          
          map_model = MapEditor::IO.load(edit_command[1])
          self.load_map(map_model, nil, edit_command[2], edit_command[3])
        end
      end
      
      def self.command
        return ['move_map', @map_model.map_id, @controler.sx, @controler.sy]
      end
      
      def self.command_to_s(model, args, margin = "")
        s = ""
        s += "Move Map"
        s +=  "(#{args[1]} : #{args[2]}, #{args[3]})"
        return s
      end
      
      def self.icon
        return @@icon
      end
    end
  end
end